package com.accompany.api.presenter;

import com.accompany.api.bean.AountMoneyBean;
import com.accompany.api.bean.ExteactMoneyBean;
import com.accompany.api.category.ExtractMoneyCategory;
import com.accompany.base.BaseBean;
import com.accompany.utils.RetrofitUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 信息说明:
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/5/11 16 ：26
 * 项目名称：yuanban
 */
public class ExtractMoneyPresenter implements ExtractMoneyCategory.Presenter {
    ExtractMoneyCategory.View view;

    public ExtractMoneyPresenter(ExtractMoneyCategory.View view) {
        this.view = view;
    }

    @Override
    public void getBankCardlist(String mToken, String requstBody) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requstBody);
        RetrofitUtil.getInstance().getmService().get_money_infor(mToken,body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String result = null;
                try {
                    result = response.body().string();
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    ExteactMoneyBean userInfor = gson.fromJson(result, ExteactMoneyBean.class);
                    view.getBankCardlist(userInfor);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                view.OnShowErrorMsg(t.getMessage());
            }
        });
    }

    @Override
    public void getMoneyPrice(String mToken, long userId) {
        RetrofitUtil.getInstance().getmService().get_account_money(mToken,String.valueOf(userId)).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String result = null;
                try {
                    result = response.body().string();
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    AountMoneyBean userInfor = gson.fromJson(result, AountMoneyBean.class);
                    view.getMoneyPrice(userInfor);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                view.OnShowErrorMsg(t.getMessage());
            }
        });
    }
}
